﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using SchoolManagementSystem.Forms.BusinessForm;
namespace SchoolManagementSystem.Details
{
    //ngành học
    public partial class BranchUC : DetailUC
    {
        public BranchUC()
        {
            InitializeComponent();
            this.ObjectName = "ngành học";
            this.Code = "Branch";
            this.Type =new Branch();

          
        }


        long BranchID;
        protected override long DetailDisplay()
        {
            Branch data = new Branch();
            if (ID > 0)
            {
                data = (Branch)RegisterService.RegisterServices.BranchService.GetByID(ID);
                BranchID = data.ParentID;
                if(data.ParentID!=0)
                buttonEdit1.EditValue = RegisterService.RegisterServices.BranchService.GetBranchByParentID(BranchID).Name;
            }
            else
            {
                BranchID = 0;
                buttonEdit1.EditValue = "";
            }
            txtname.Text = data.Name;
            txtnote.Text = data.Description;
            txtcode.Text = data.Code;
            
            ckactive.Checked = data.Active;

            return ID;

        }

        protected override long Save()
        {
            Branch data = new Branch();
            if (ID > 0)
            {
                data = (Branch)RegisterService.RegisterServices.BranchService.GetByID(ID);

            }
            data.Code = txtcode.Text;
            data.Name = txtname.Text;
            data.Description = txtnote.Text;
            data.Active = ckactive.Checked;
            data.ParentID = BranchID;
            ID = RegisterService.RegisterServices.BranchService.Save(data, ref ErrorMess);

            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            var check = RegisterService.RegisterServices.ClassService.GetByBranchID(SelectedID);
            if (check != null && check.Count != 0)
            {
                ErrorMess = "Vui lòng xóa các lớp học liên quan trước khi xóa ngành học này";
                return false;
            }
            else
            {
                //return RegisterService.RegisterServices.BranchService.Delete(RegisterService.RegisterServices.BranchService.GetByID(SelectedID));
                return RegisterService.RegisterServices.BranchService.UpdateIsDelete(RegisterService.RegisterServices.BranchService.GetByID(SelectedID));
            }
        }


        private void BranchUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(groupControl1.Controls);
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmSelectBranchParent br = new frmSelectBranchParent();
            if(br.ShowDialog() ==DialogResult.OK)
            {
                buttonEdit1.EditValue = br.Name;
                BranchID = br.ID;
            }
            else
            {
                if (String.IsNullOrEmpty((string)buttonEdit1.EditValue))
                {
                    buttonEdit1.EditValue = "";
                    BranchID = 0;
                }
            }
        }
    }
}
